clear;
clc;

format short

data      = load('matlab_raw.csv',',');
data      = data(:,1:end-1);
I0        = min(data(:,9));
I1        = max(data(:,9));
%{
pause(1+60*rand())

c=parcluster();
t=tempname();
mkdir(t);
c.JobStorageLocation=t;

myCluster = parcluster('local');
myCluster.NumWorkers = 4;

currentfolder = pwd;
sched=findResource('scheduler','type','local');
sched.DataLocation = currentfolder;


matlabpool close force
matlabpool open 4
%}
tic
parfor x = I0:I1
    s = RandStream('CombRecursive','Seed',1);
    RandStream.setGlobalStream(s);
    id0       = find(data(:,9)==x);
    
    id        = find(data(:,9)==x)
    data2     = data(id,:);
    fup12     = data2(:,1);
    fid       = data2(1,2);
    year      = data2(:,3);
    a         = data2(:,4);
    b         = data2(:,5);
    c         = data2(:,6);
    svshare   = data2(:,7);
    N         = data2(1,8);
    fid2yr    = data2(1,9);
    omega1    = data2(:,end);
        
    abc      = [a b c];
    
    %%4 different starting values
    %seed = 1111;
    %rng(seed);
	    x0a = rand(1,1);
	    x0b = (1 - x0a)/(N-1);
	    x0 = [x0a ; repmat(x0b,(N-1),1)];
	    x0_1_1      = [rand(1,1) ; x0];

    %revenue shares
    %seed = 1112;
    %rng(seed);
    x0_1_2      = [rand(1,1) ; svshare];
   
    %random
    %seed = 1113;
    %rng(seed);
    x0_1_3      = [rand(1,1) ; rand(length(svshare),1)];
  
    %1/N
    %seed = 1114;
    %rng(seed);
    x0_1_4      = [rand(1,1) ; ones(N,1)/N];
    
    dataout = runrho11(x,fup12,fid,year,a,b,c,svshare,N,fid2yr,omega1,abc,x0_1_1,x0_1_2,x0_1_3,x0_1_4);
    
end
toc
%matlabpool close

lock = 1;
csvwrite('lock.out',lock);

exit
